// base
import Taro, { Component } from '@tarojs/taro'
import { View, Button, Form } from '@tarojs/components'
import { connect } from '@tarojs/redux'
// less
import './index.less'
import ButtonSelf from '../button-self'
import messagePush from '../../../util/messagePush'
import tool from '../../../util/tool'

@connect(
  userinfo => ({ ...userinfo }),
  dispatch => ({})
)
class BtmFixedBtn extends Component {
  constructor (props) {
    super(props)
    this.state = { canTab: true }
  }
  componentWillMount () {
    // onLoad
  }

  componentDidMount () {
    // onready
  }

  componentDidShow () {
    // onshow
  }

  componentDidHide () {
    // onhide
  }

  componentWillUnmount () {
    // onUnload
    this.taptimer && clearTimeout(this.taptimer)
  }

  delay (time = 0) {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve()
      }, time)
    })
  }
  taptimer = null

  onTap = tool.throttle(() => {
    if (this.props.cant) {
      return
    }
    if (this.state.canTab) {
      this.setState({
        canTab: false
      })
      this.props.onTap()
      this.taptimer = setTimeout(() => {
        this.setState({
          canTab: true
        })
        clearTimeout(this.taptimer)
      }, 1500)
    }
  })

  formSubmit = e => {
    let { formId } = e.detail
    const { openId, userId } = this.props.userinfo
    messagePush.insertFormId({ userId, formId, openId })
  }

  render () {
    const { cant } = this.props
    return (
      <View className="BtmFixedBtn_id92735">
        <Form onSubmit={this.formSubmit} reportSubmit>
          <Button formType="submit" className="form-btn" onClick={this.onTap}>
            <ButtonSelf text={this.props.text} cant={cant} />
          </Button>
        </Form>
      </View>
    )
  }
}

export default BtmFixedBtn
